//
// detrendIBI_priors.cpp
//
// Code generation for function 'detrendIBI_priors'
//

// Include files
#include "detrendIBI_priors.h"
#include "detrendIBI_priors_data.h"
#include "interp1.h"
#include "rt_nonfinite.h"
#include "sparse1.h"
#include "spdiags.h"
#include "speye.h"
#include "sumMatrixIncludeNaN.h"
#include "coder_array.h"
#include "mwmathutil.h"
#include "omp.h"

// Variable Definitions
static emlrtRSInfo emlrtRSI{
    5,                   // lineNo
    "detrendIBI_priors", // fcnName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pathName
};

static emlrtRSInfo b_emlrtRSI{
    9,                   // lineNo
    "detrendIBI_priors", // fcnName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pathName
};

static emlrtRSInfo c_emlrtRSI{
    12,                  // lineNo
    "detrendIBI_priors", // fcnName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pathName
};

static emlrtRSInfo d_emlrtRSI{
    13,                  // lineNo
    "detrendIBI_priors", // fcnName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pathName
};

static emlrtRSInfo e_emlrtRSI{
    15,                  // lineNo
    "detrendIBI_priors", // fcnName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pathName
};

static emlrtRSInfo f_emlrtRSI{
    16,                  // lineNo
    "detrendIBI_priors", // fcnName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pathName
};

static emlrtRSInfo g_emlrtRSI{
    17,                  // lineNo
    "detrendIBI_priors", // fcnName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pathName
};

static emlrtRSInfo h_emlrtRSI{
    20,                  // lineNo
    "detrendIBI_priors", // fcnName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pathName
};

static emlrtRSInfo i_emlrtRSI{
    21,                  // lineNo
    "detrendIBI_priors", // fcnName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pathName
};

static emlrtRSInfo j_emlrtRSI{
    26,    // lineNo
    "cat", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\eml\\+coder\\+internal\\cat.m" // pathName
};

static emlrtRSInfo k_emlrtRSI{
    100,        // lineNo
    "cat_impl", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\eml\\+coder\\+internal\\cat.m" // pathName
};

static emlrtRSInfo l_emlrtRSI{
    49,     // lineNo
    "mean", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\datafun\\mean.m" // pathName
};

static emlrtRSInfo m_emlrtRSI{
    74,                      // lineNo
    "combineVectorElements", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\datafun\\private\\combin"
    "eVectorElements.m" // pathName
};

static emlrtRSInfo n_emlrtRSI{
    107,                // lineNo
    "blockedSummation", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\datafun\\private\\blocke"
    "dSummation.m" // pathName
};

static emlrtRSInfo o_emlrtRSI{
    22,                    // lineNo
    "sumMatrixIncludeNaN", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\datafun\\private\\sumMat"
    "rixIncludeNaN.m" // pathName
};

static emlrtRSInfo p_emlrtRSI{
    42,                 // lineNo
    "sumMatrixColumns", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\datafun\\private\\sumMat"
    "rixIncludeNaN.m" // pathName
};

static emlrtRSInfo r_emlrtRSI{
    57,                 // lineNo
    "sumMatrixColumns", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\datafun\\private\\sumMat"
    "rixIncludeNaN.m" // pathName
};

static emlrtRSInfo
    cb_emlrtRSI{
        328,               // lineNo
        "eml_float_colon", // fcnName
        "D:\\Program "
        "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\ops\\colon.m" // pathName
    };

static emlrtRSInfo vd_emlrtRSI{
    44,       // lineNo
    "mpower", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\matfun\\mpower.m" // pathName
};

static emlrtRSInfo
    wd_emlrtRSI{
        71,      // lineNo
        "power", // fcnName
        "D:\\Program "
        "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\ops\\power.m" // pathName
    };

static emlrtRSInfo qe_emlrtRSI{
    18,              // lineNo
    "sparse/mtimes", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\shared\\coder\\coder\\+coder\\+internal\\@"
    "sparse\\mtimes.m" // pathName
};

static emlrtRTEInfo c_emlrtRTEI{
    275,                   // lineNo
    27,                    // colNo
    "check_non_axis_size", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\eml\\+coder\\+internal\\cat.m" // pName
};

static emlrtECInfo emlrtECI{
    -1,                  // nDims
    29,                  // lineNo
    10,                  // colNo
    "detrendIBI_priors", // fName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pName
};

static emlrtECInfo b_emlrtECI{
    1,                   // nDims
    21,                  // lineNo
    13,                  // colNo
    "detrendIBI_priors", // fName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pName
};

static emlrtECInfo c_emlrtECI{
    -1,                  // nDims
    20,                  // lineNo
    5,                   // colNo
    "detrendIBI_priors", // fName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pName
};

static emlrtECInfo d_emlrtECI{
    -1,                  // nDims
    7,                   // lineNo
    5,                   // colNo
    "detrendIBI_priors", // fName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pName
};

static emlrtECInfo e_emlrtECI{
    1,                   // nDims
    18,                  // lineNo
    8,                   // colNo
    "detrendIBI_priors", // fName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pName
};

static emlrtDCInfo emlrtDCI{
    16,                  // lineNo
    21,                  // colNo
    "detrendIBI_priors", // fName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m", // pName
    4 // checkKind
};

static emlrtBCInfo emlrtBCI{
    -1,                  // iFirst
    -1,                  // iLast
    12,                  // lineNo
    22,                  // colNo
    "t",                 // aName
    "detrendIBI_priors", // fName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m", // pName
    0 // checkKind
};

static emlrtBCInfo b_emlrtBCI{
    -1,                  // iFirst
    -1,                  // iLast
    12,                  // lineNo
    12,                  // colNo
    "t",                 // aName
    "detrendIBI_priors", // fName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m", // pName
    0 // checkKind
};

static emlrtRTEInfo pb_emlrtRTEI{
    5,                   // lineNo
    5,                   // colNo
    "detrendIBI_priors", // fName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pName
};

static emlrtRTEInfo qb_emlrtRTEI{
    6,                   // lineNo
    5,                   // colNo
    "detrendIBI_priors", // fName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pName
};

static emlrtRTEInfo rb_emlrtRTEI{
    2,                   // lineNo
    7,                   // colNo
    "detrendIBI_priors", // fName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pName
};

static emlrtRTEInfo tb_emlrtRTEI{
    12,                  // lineNo
    5,                   // colNo
    "detrendIBI_priors", // fName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pName
};

static emlrtRTEInfo ub_emlrtRTEI{
    3,                   // lineNo
    7,                   // colNo
    "detrendIBI_priors", // fName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pName
};

static emlrtRTEInfo vb_emlrtRTEI{
    13,                  // lineNo
    5,                   // colNo
    "detrendIBI_priors", // fName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pName
};

static emlrtRTEInfo
    wb_emlrtRTEI{
        88,                  // lineNo
        13,                  // colNo
        "eml_mtimes_helper", // fName
        "D:\\Program "
        "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\ops\\eml_mtimes_"
        "helper.m" // pName
    };

static emlrtRTEInfo xb_emlrtRTEI{
    20,                  // lineNo
    16,                  // colNo
    "detrendIBI_priors", // fName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pName
};

static emlrtRTEInfo yb_emlrtRTEI{
    21,                  // lineNo
    13,                  // colNo
    "detrendIBI_priors", // fName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pName
};

static emlrtRTEInfo ac_emlrtRTEI{
    21,                  // lineNo
    5,                   // colNo
    "detrendIBI_priors", // fName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pName
};

static emlrtRTEInfo bc_emlrtRTEI{
    29,                  // lineNo
    22,                  // colNo
    "detrendIBI_priors", // fName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pName
};

static emlrtRTEInfo ae_emlrtRTEI{
    18,                  // lineNo
    8,                   // colNo
    "detrendIBI_priors", // fName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pName
};

static emlrtRSInfo di_emlrtRSI{
    18,                  // lineNo
    "detrendIBI_priors", // fcnName
    "E:\\MCode\\matlab_code\\HRVAS_v1.0.0\\chaifen\\detrendIBI_priors.m" // pathName
};

// Function Declarations
static void binary_expand_op(const emlrtStack *sp,
                             coder::array<real_T, 1U> &in1,
                             const coder::array<real_T, 2U> &in2,
                             const coder::array<real_T, 2U> &in3);

static void minus(const emlrtStack *sp, coder::array<real_T, 1U> &in1,
                  const coder::array<real_T, 1U> &in2);

// Function Definitions
static void binary_expand_op(const emlrtStack *sp,
                             coder::array<real_T, 1U> &in1,
                             const coder::array<real_T, 2U> &in2,
                             const coder::array<real_T, 2U> &in3)
{
  int32_T b_loop_ub;
  int32_T i;
  int32_T loop_ub;
  int32_T stride_1_0;
  i = in2.size(0);
  loop_ub = in3.size(0);
  if (loop_ub == 1) {
    b_loop_ub = i;
  } else {
    b_loop_ub = loop_ub;
  }
  in1.set_size(&yb_emlrtRTEI, sp, b_loop_ub);
  b_loop_ub = (i != 1);
  stride_1_0 = (loop_ub != 1);
  if (loop_ub == 1) {
    loop_ub = i;
  }
  for (i = 0; i < loop_ub; i++) {
    in1[i] =
        in2[i * b_loop_ub + in2.size(0)] - in3[i * stride_1_0 + in3.size(0)];
  }
}

static void minus(const emlrtStack *sp, coder::array<real_T, 1U> &in1,
                  const coder::array<real_T, 1U> &in2)
{
  coder::array<real_T, 1U> b_in1;
  int32_T i;
  int32_T loop_ub;
  int32_T stride_0_0;
  int32_T stride_1_0;
  emlrtHeapReferenceStackEnterFcnR2012b((emlrtCTX)sp);
  if (in2.size(0) == 1) {
    i = in1.size(0);
  } else {
    i = in2.size(0);
  }
  b_in1.set_size(&ae_emlrtRTEI, sp, i);
  stride_0_0 = (in1.size(0) != 1);
  stride_1_0 = (in2.size(0) != 1);
  if (in2.size(0) == 1) {
    loop_ub = in1.size(0);
  } else {
    loop_ub = in2.size(0);
  }
  for (i = 0; i < loop_ub; i++) {
    b_in1[i] = in1[i * stride_0_0] - in2[i * stride_1_0];
  }
  in1.set_size(&ae_emlrtRTEI, sp, b_in1.size(0));
  loop_ub = b_in1.size(0);
  for (i = 0; i < loop_ub; i++) {
    in1[i] = b_in1[i];
  }
  emlrtHeapReferenceStackLeaveFcnR2012b((emlrtCTX)sp);
}

void detrendIBI_priors(const emlrtStack *sp,
                       const coder::array<real_T, 2U> &ibi, real_T resampleRate,
                       real_T lambda, boolean_T meanCorrection,
                       coder::array<real_T, 2U> &dibi,
                       coder::array<real_T, 2U> &nibi,
                       coder::array<real_T, 2U> &trend)
{
  static const int8_T b[3]{1, -2, 1};
  coder::sparse D2;
  coder::sparse b_I;
  coder::sparse b_b;
  coder::sparse r2;
  coder::array<real_T, 2U> r;
  coder::array<real_T, 2U> r1;
  coder::array<real_T, 2U> t2;
  coder::array<real_T, 1U> b_ibi;
  coder::array<real_T, 1U> y2;
  coder::array<real_T, 1U> z_stat;
  emlrtStack b_st;
  emlrtStack c_st;
  emlrtStack d_st;
  emlrtStack e_st;
  emlrtStack f_st;
  emlrtStack st;
  real_T d;
  real_T meanIbi;
  real_T s;
  int32_T inb;
  int32_T loop_ub;
  int32_T nfb;
  int32_T nleft;
  st.prev = sp;
  st.tls = sp->tls;
  b_st.prev = &st;
  b_st.tls = st.tls;
  c_st.prev = &b_st;
  c_st.tls = b_st.tls;
  d_st.prev = &c_st;
  d_st.tls = c_st.tls;
  e_st.prev = &d_st;
  e_st.tls = d_st.tls;
  f_st.prev = &e_st;
  f_st.tls = e_st.tls;
  emlrtHeapReferenceStackEnterFcnR2012b((emlrtCTX)sp);
  covrtLogFcn(&emlrtCoverageInstance, 0, 0);
  covrtLogBasicBlock(&emlrtCoverageInstance, 0, 0);
  //  preallocate memory and create default trend of 0.
  st.site = &emlrtRSI;
  b_st.site = &j_emlrtRSI;
  c_st.site = &k_emlrtRSI;
  loop_ub = ibi.size(0);
  nfb = ibi.size(0);
  nibi.set_size(&pb_emlrtRTEI, &b_st, ibi.size(0), 2);
  for (int32_T i{0}; i < loop_ub; i++) {
    nibi[i] = ibi[i];
  }
  for (int32_T i{0}; i < nfb; i++) {
    nibi[i + nibi.size(0)] = ibi[i + ibi.size(0)];
  }
  trend.set_size(&qb_emlrtRTEI, sp, ibi.size(0), 2);
  loop_ub = ibi.size(0) << 1;
  for (int32_T i{0}; i < loop_ub; i++) {
    trend[i] = 0.0;
  }
  emlrtSubAssignSizeCheckR2012b(((coder::array<real_T, 2U> *)&ibi)->size(), 1,
                                ((coder::array<real_T, 2U> *)&ibi)->size(), 1,
                                &d_emlrtECI, (emlrtCTX)sp);
  loop_ub = ibi.size(0);
  for (int32_T i{0}; i < loop_ub; i++) {
    trend[i] = ibi[i];
  }
  // time values
  st.site = &b_emlrtRSI;
  b_st.site = &l_emlrtRSI;
  c_st.site = &m_emlrtRSI;
  if (ibi.size(0) == 0) {
    s = 0.0;
  } else {
    d_st.site = &n_emlrtRSI;
    e_st.site = &o_emlrtRSI;
    if (ibi.size(0) < 4096) {
      loop_ub = ibi.size(0);
      b_ibi.set_size(&rb_emlrtRTEI, &e_st, ibi.size(0));
      for (int32_T i{0}; i < loop_ub; i++) {
        b_ibi[i] = ibi[i + ibi.size(0)];
      }
      f_st.site = &p_emlrtRSI;
      s = coder::sumColumnB(&f_st, b_ibi, ibi.size(0));
    } else {
      nfb = ibi.size(0) / 4096;
      inb = nfb << 12;
      nleft = ibi.size(0) - inb;
      loop_ub = ibi.size(0);
      b_ibi.set_size(&rb_emlrtRTEI, &e_st, ibi.size(0));
      for (int32_T i{0}; i < loop_ub; i++) {
        b_ibi[i] = ibi[i + ibi.size(0)];
      }
      s = coder::sumColumnB4(b_ibi, 1);
      for (int32_T ib{2}; ib <= nfb; ib++) {
        loop_ub = ibi.size(0);
        b_ibi.set_size(&rb_emlrtRTEI, &e_st, ibi.size(0));
        for (int32_T i{0}; i < loop_ub; i++) {
          b_ibi[i] = ibi[i + ibi.size(0)];
        }
        s += coder::sumColumnB4(b_ibi, ((ib - 1) << 12) + 1);
      }
      if (nleft > 0) {
        loop_ub = ibi.size(0);
        b_ibi.set_size(&rb_emlrtRTEI, &e_st, ibi.size(0));
        for (int32_T i{0}; i < loop_ub; i++) {
          b_ibi[i] = ibi[i + ibi.size(0)];
        }
        f_st.site = &r_emlrtRSI;
        s += coder::sumColumnB(&f_st, b_ibi, nleft, inb + 1);
      }
    }
  }
  meanIbi = s / static_cast<real_T>(ibi.size(0));
  // WARNING: cutoff freq depends on lambda and resample rate
  // resample rate
  if (ibi.size(0) < 1) {
    emlrtDynamicBoundsCheckR2012b(1, 1, ibi.size(0), &b_emlrtBCI, (emlrtCTX)sp);
  }
  st.site = &c_emlrtRSI;
  d = 1.0 / resampleRate;
  if (ibi.size(0) < 1) {
    emlrtDynamicBoundsCheckR2012b(ibi.size(0), 1, ibi.size(0), &emlrtBCI, &st);
  }
  if (muDoubleScalarIsNaN(ibi[0]) || muDoubleScalarIsNaN(d) ||
      muDoubleScalarIsNaN(ibi[ibi.size(0) - 1])) {
    t2.set_size(&tb_emlrtRTEI, &st, 1, 1);
    t2[0] = rtNaN;
  } else if ((d == 0.0) || ((ibi[0] < ibi[ibi.size(0) - 1]) && (d < 0.0)) ||
             ((ibi[ibi.size(0) - 1] < ibi[0]) && (d > 0.0))) {
    t2.set_size(&tb_emlrtRTEI, &st, 1, 0);
  } else if ((muDoubleScalarIsInf(ibi[0]) ||
              muDoubleScalarIsInf(ibi[ibi.size(0) - 1])) &&
             (muDoubleScalarIsInf(d) || (ibi[0] == ibi[ibi.size(0) - 1]))) {
    t2.set_size(&tb_emlrtRTEI, &st, 1, 1);
    t2[0] = rtNaN;
  } else if (muDoubleScalarIsInf(d)) {
    t2.set_size(&tb_emlrtRTEI, &st, 1, 1);
    t2[0] = ibi[0];
  } else if ((muDoubleScalarFloor(ibi[0]) == ibi[0]) &&
             (muDoubleScalarFloor(d) == d)) {
    t2.set_size(&tb_emlrtRTEI, &st, 1,
                static_cast<int32_T>((ibi[ibi.size(0) - 1] - ibi[0]) / d) + 1);
    loop_ub = static_cast<int32_T>((ibi[ibi.size(0) - 1] - ibi[0]) / d);
    for (int32_T i{0}; i <= loop_ub; i++) {
      t2[i] = ibi[0] + d * static_cast<real_T>(i);
    }
  } else {
    real_T apnd;
    real_T cdiff;
    b_st.site = &ab_emlrtRSI;
    s = muDoubleScalarFloor((ibi[ibi.size(0) - 1] - ibi[0]) / d + 0.5);
    apnd = ibi[0] + s * d;
    if (d > 0.0) {
      cdiff = apnd - ibi[ibi.size(0) - 1];
    } else {
      cdiff = ibi[ibi.size(0) - 1] - apnd;
    }
    if (muDoubleScalarAbs(cdiff) <
        4.4408920985006262E-16 *
            muDoubleScalarMax(muDoubleScalarAbs(ibi[0]),
                              muDoubleScalarAbs(ibi[ibi.size(0) - 1]))) {
      s++;
      apnd = ibi[ibi.size(0) - 1];
    } else if (cdiff > 0.0) {
      apnd = ibi[0] + (s - 1.0) * d;
    } else {
      s++;
    }
    if (s >= 0.0) {
      nfb = static_cast<int32_T>(s);
    } else {
      nfb = 0;
    }
    c_st.site = &bb_emlrtRSI;
    if (s > 2.147483647E+9) {
      emlrtErrorWithMessageIdR2018a(&c_st, &emlrtRTEI, "Coder:MATLAB:pmaxsize",
                                    "Coder:MATLAB:pmaxsize", 0);
    }
    t2.set_size(&sb_emlrtRTEI, &b_st, 1, nfb);
    if (nfb > 0) {
      t2[0] = ibi[0];
      if (nfb > 1) {
        t2[nfb - 1] = apnd;
        inb = (nfb - 1) / 2;
        c_st.site = &cb_emlrtRSI;
        for (nleft = 0; nleft <= inb - 2; nleft++) {
          s = (static_cast<real_T>(nleft) + 1.0) * d;
          t2[nleft + 1] = ibi[0] + s;
          t2[(nfb - nleft) - 2] = apnd - s;
        }
        if (inb << 1 == nfb - 1) {
          t2[inb] = (ibi[0] + apnd) / 2.0;
        } else {
          s = static_cast<real_T>(inb) * d;
          t2[inb] = ibi[0] + s;
          t2[inb + 1] = apnd - s;
        }
      }
    }
  }
  // time values for interp.
  loop_ub = ibi.size(0);
  b_ibi.set_size(&ub_emlrtRTEI, sp, ibi.size(0));
  for (int32_T i{0}; i < loop_ub; i++) {
    b_ibi[i] = ibi[i];
  }
  loop_ub = ibi.size(0);
  y2.set_size(&rb_emlrtRTEI, sp, ibi.size(0));
  for (int32_T i{0}; i < loop_ub; i++) {
    y2[i] = ibi[i + ibi.size(0)];
  }
  st.site = &d_emlrtRSI;
  coder::interp1(&st, b_ibi, y2, t2, r);
  y2.set_size(&vb_emlrtRTEI, sp, r.size(1));
  loop_ub = r.size(1);
  for (int32_T i{0}; i < loop_ub; i++) {
    y2[i] = r[i];
  }
  // resample
  st.site = &e_emlrtRSI;
  coder::speye(&st, static_cast<real_T>(y2.size(0)), &b_I);
  if (!(static_cast<real_T>(y2.size(0)) - 2.0 >= 0.0)) {
    emlrtNonNegativeCheckR2012b(static_cast<real_T>(y2.size(0)) - 2.0,
                                &emlrtDCI, (emlrtCTX)sp);
  }
  r1.set_size(&wb_emlrtRTEI, sp, y2.size(0) - 2, 3);
  loop_ub = y2.size(0) - 2;
  for (int32_T i{0}; i < 3; i++) {
    for (nfb = 0; nfb < loop_ub; nfb++) {
      r1[nfb + r1.size(0) * i] = b[i];
    }
  }
  st.site = &f_emlrtRSI;
  coder::spdiags(&st, r1, static_cast<real_T>(y2.size(0)) - 2.0,
                 static_cast<real_T>(y2.size(0)), &D2);
  st.site = &g_emlrtRSI;
  b_st.site = &vd_emlrtRSI;
  c_st.site = &wd_emlrtRSI;
  st.site = &g_emlrtRSI;
  b_st.site = &g_emlrtRSI;
  D2.ctranspose(&b_st, &r2);
  b_st.site = &g_emlrtRSI;
  r2.mtimes(&b_st, &D2, &b_b);
  b_st.site = &qe_emlrtRSI;
  b_b.times(&b_st, lambda * lambda, &D2);
  st.site = &g_emlrtRSI;
  b_I.plus(&st, &D2, &r2);
  st.site = &g_emlrtRSI;
  r2.inv(&st, &D2);
  st.site = &g_emlrtRSI;
  b_I.b_minus(&st, &D2, &r2);
  st.site = &g_emlrtRSI;
  r2.mtimes(&st, y2, z_stat);
  // detrened signal
  if ((y2.size(0) != z_stat.size(0)) &&
      ((y2.size(0) != 1) && (z_stat.size(0) != 1))) {
    emlrtDimSizeImpxCheckR2021b(y2.size(0), z_stat.size(0), &e_emlrtECI,
                                (emlrtCTX)sp);
  }
  if (y2.size(0) == z_stat.size(0)) {
    loop_ub = y2.size(0);
    for (int32_T i{0}; i < loop_ub; i++) {
      y2[i] = y2[i] - z_stat[i];
    }
  } else {
    st.site = &di_emlrtRSI;
    minus(&st, y2, z_stat);
  }
  // trend
  // resample trend back to original time points
  loop_ub = ibi.size(0);
  b_ibi.set_size(&ub_emlrtRTEI, sp, ibi.size(0));
  for (int32_T i{0}; i < loop_ub; i++) {
    b_ibi[i] = ibi[i];
  }
  st.site = &h_emlrtRSI;
  coder::interp1(&st, t2, y2, b_ibi, z_stat);
  r.set_size(&xb_emlrtRTEI, sp, 1, z_stat.size(0));
  loop_ub = z_stat.size(0);
  for (int32_T i{0}; i < loop_ub; i++) {
    r[i] = z_stat[i];
  }
  emlrtSubAssignSizeCheckR2012b(trend.size(), 1, r.size(), 2, &c_emlrtECI,
                                (emlrtCTX)sp);
  loop_ub = trend.size(0);
  for (int32_T i{0}; i < loop_ub; i++) {
    trend[i + trend.size(0)] = r[i];
  }
  // interpolation
  if ((ibi.size(0) != trend.size(0)) &&
      ((ibi.size(0) != 1) && (trend.size(0) != 1))) {
    emlrtDimSizeImpxCheckR2021b(ibi.size(0), trend.size(0), &b_emlrtECI,
                                (emlrtCTX)sp);
  }
  st.site = &i_emlrtRSI;
  if (ibi.size(0) == trend.size(0)) {
    loop_ub = ibi.size(0);
    z_stat.set_size(&yb_emlrtRTEI, &st, ibi.size(0));
    for (int32_T i{0}; i < loop_ub; i++) {
      z_stat[i] = ibi[i + ibi.size(0)] - trend[i + trend.size(0)];
    }
  } else {
    b_st.site = &i_emlrtRSI;
    binary_expand_op(&b_st, z_stat, ibi, trend);
  }
  b_st.site = &j_emlrtRSI;
  c_st.site = &k_emlrtRSI;
  if (z_stat.size(0) != ibi.size(0)) {
    emlrtErrorWithMessageIdR2018a(&c_st, &c_emlrtRTEI,
                                  "MATLAB:catenate:matrixDimensionMismatch",
                                  "MATLAB:catenate:matrixDimensionMismatch", 0);
  }
  loop_ub = ibi.size(0);
  dibi.set_size(&ac_emlrtRTEI, &b_st, ibi.size(0), 2);
  for (int32_T i{0}; i < loop_ub; i++) {
    dibi[i] = ibi[i];
  }
  loop_ub = z_stat.size(0);
  for (int32_T i{0}; i < loop_ub; i++) {
    dibi[i + dibi.size(0)] = z_stat[i];
  }
  // detrended IBI
  // %% Note: After removing the trend, the mean value of the ibi
  //  series is near zero and some of the artifact detection methods
  //  detect two many outlers due to a mean near zero
  //  Solution: shift detrended ibi back up to it's orignal mean
  //  by adding orignal mean to detrended ibi.
  if (covrtLogIf(&emlrtCoverageInstance, 0, 0, 0, meanCorrection)) {
    covrtLogBasicBlock(&emlrtCoverageInstance, 0, 1);
    emlrtSubAssignSizeCheckR2012b(dibi.size(), 1, dibi.size(), 1, &emlrtECI,
                                  (emlrtCTX)sp);
    nfb = dibi.size(0) - 1;
    y2.set_size(&bc_emlrtRTEI, sp, dibi.size(0));
    for (int32_T i{0}; i <= nfb; i++) {
      y2[i] = dibi[i + dibi.size(0)] + meanIbi;
    }
    loop_ub = y2.size(0);
    for (int32_T i{0}; i < loop_ub; i++) {
      dibi[i + dibi.size(0)] = y2[i];
    }
    //  see note above
  }
  emlrtHeapReferenceStackLeaveFcnR2012b((emlrtCTX)sp);
}

emlrtCTX emlrtGetRootTLSGlobal()
{
  return emlrtRootTLSGlobal;
}

void emlrtLockerFunction(EmlrtLockeeFunction aLockee, emlrtConstCTX aTLS,
                         void *aData)
{
  omp_set_lock(&emlrtLockGlobal);
  emlrtCallLockeeFunction(aLockee, aTLS, aData);
  omp_unset_lock(&emlrtLockGlobal);
}

// End of code generation (detrendIBI_priors.cpp)
